package com.tsj.spzx.user.service.Impl;

import com.tsj.spzx.model.entity.user.UserAddress;
import com.tsj.spzx.user.mapper.UserAddressMapper;
import com.tsj.spzx.user.service.UserAddressService;
import com.tsj.spzx.utils.AuthContextThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    /**
     * 查询用户的地址列表
     * @return
     */
    @Override
    public List<UserAddress> findUserAddressList() {
        // 获取到用户Id
        Long userId = AuthContextThreadLocalUtil.getUserInfo().getId();
        List<UserAddress> userAddressList = userAddressMapper.findUserAddressList(userId);
        return userAddressList;
    }

    /**
     * 新增用户地址
     * @param userAddress
     */
    @Override
    public void save(UserAddress userAddress) {
        // 获取用户userId
        Long userId = AuthContextThreadLocalUtil.getUserInfo().getId();
        userAddress.setUserId(userId);
        // 设置fullAddress
        String provinceName = userAddressMapper.findRegionName(userAddress.getProvinceCode());
        String cityName = userAddressMapper.findRegionName(userAddress.getCityCode());
        String countyName = userAddressMapper.findRegionName(userAddress.getDistrictCode());
        userAddress.setFullAddress(provinceName + cityName + countyName + userAddress.getAddress());

        userAddressMapper.save(userAddress);
    }

    /**
     * 修改用户地址
     * @param userAddress
     */
    @Override
    public void updateById(UserAddress userAddress) {
        userAddressMapper.updateById(userAddress);
    }

    /**
     * 删除用户地址
     * @param id
     */
    @Override
    public void removeById(Long id) {
        userAddressMapper.removeById(id);
    }

    /**
     * 根据id查询用户地址
     * @param id
     * @return
     */
    @Override
    public UserAddress getAddressById(Long id) {
        UserAddress userAddress =userAddressMapper.getAddressById(id);
        return userAddress;
    }
}
